
import json
import requests
import os
import win32api,win32con


class alert_dialog(object):

    def __init__(self):
        self.headers = {'Content-Type': 'application/json'}

    def show(self, user_name_list):

        if os.path.exists("../user_setting/setting.json"):
            with open("../user_setting/setting.json", "r")as f:
                data = json.loads(f.read())
        checkState = data['checkState']
        if not checkState:
            # 查找有无异常信息
            except_msg = self.get_exception_msg(user_name_list)
            if len(except_msg) > 0:
                win32api.MessageBox(0, except_msg, "提示", win32con.MB_TOPMOST)
                # 从队列移除数据
                self.remove_exception_msg(user_name_list)

    # 从队列里获取异常信息
    def get_exception_msg(self, user_name_list):
        response = requests.post("http://localhost:8905/order/queryMessage", data=json.dumps(user_name_list), headers=self.headers)
        return json.loads(response.text)["content"]

    # 从队列移除异常信息
    def remove_exception_msg(self, user_name_list):
        response = requests.post("http://localhost:8905/order/removeMessage", data=json.dumps(user_name_list), headers=self.headers)